/*
 * Copyright 2007 Jeffrey Palm
 */

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

final class GoogleDirections extends AbstractFinderIPod {

  public String name() {return "Google Directions";}

  class InputPanel extends JPanel {

    private JTextField urlField = new JTextField(40);
    private JTextField fromField = new JTextField(40);
    private JTextField toField = new JTextField(40);

    private JPanel wrap(String title, Component c) {
      JPanel p = new JPanel();
      p.add(c);
      p.setBorder(BorderFactory.createTitledBorder(title));
      return p;
    }

    InputPanel() {
      setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
      add(new JLabel("Please provide a URL"));
      add(wrap("URL",urlField));
      add(new JLabel("-or- "));
      add(new JLabel("Provide 'from' and 'to' locations'"));
      add(wrap("From",fromField));
      add(wrap("To",toField));
      
    }

    public String getURL() { return urlField.getText().trim();}
    public String getTo() { return toField.getText().trim();}
    public String getFrom() { return fromField.getText().trim();}
  }

  protected void findRest(IPod ipod, Progress p) throws Exception {
    //
    // Prompt for a URL
    //
    String url = null;
    String from = null;
    String to = null;
    InputPanel ip = new InputPanel();
    while (url == null || (to == null && from == null)) {
      int res = JOptionPane.showConfirmDialog(null, ip, "Directions", 
                                              JOptionPane.OK_CANCEL_OPTION,
                                              JOptionPane.QUESTION_MESSAGE,
                                              null);
      if (res == JOptionPane.CANCEL_OPTION) return;
      url = ip.getURL();
      from = ip.getFrom();
      to = ip.getTo();
    }

    if (url == null) {
      url = createURL(from,to);
    }
    
    BufferedReader in = new BufferedReader(new InputStreamReader(open(url)));
    String line;
    while ((line = in.readLine()) != null) {
      System.out.println(line);
    }
    in.close();
  }

  private String createURL(String from, String to) {
    return null;
  }

  public static void main(String[] args) throws Exception {
    new GoogleDirections().find(new IPod(),Progress.DEFAULT);
    System.exit(0);
  }

}